package servlet;

import dbOperation.DbUpdate;
import library.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by v on 17-5-23.
 */
@WebServlet(name = "UpdataServlet",value = "/UpdataServlet")
public class UpdataServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Book book=new Book();
        String page=req.getParameter("page");
        try {
            book.setId(Integer.parseInt(req.getParameter("id")));
            book.setAuthor(req.getParameter("author"));
            book.setAmount(Integer.parseInt(req.getParameter("amount")));
            book.setPrice(Double.valueOf(req.getParameter("price")));
            book.setName(req.getParameter("name"));
        }catch (Exception ex) {
            resp.sendRedirect("/exception/updata.jsp?page="+page);
            return;
        }

        if(DbUpdate.Update(book)){
            resp.sendRedirect("/accept/updata.jsp?page="+page);
        }else {
            resp.sendRedirect("/exception/updata.jsp?page="+page);
        }
    }
}